<?php

namespace app\api\controller\xykeep;

use app\common\controller\Api;
use app\common\model\CardOrder as CardOrderModel;
use app\common\model\BenfitLog as BenfitLogModel;
use app\common\model\UserCard as UserCardModel;
use app\api\model\xykeep\Goods as GoodsModel;
use app\common\model\UserCardBenfit as UserCardBenfitModel;

class BenfitOrder extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //权益卡订单列表
    public function order_lists()
    {
        $user_id = $this->auth->id;
        $data = CardOrderModel::where([
            'user_id' => $user_id,
            'status' => 1
        ])->with(['card','userCard'])->order('id desc')->paginate();
        $this->success('',$data);
    }

    public function order_detail()
    {
        $id = input('id/d',0);
        $user_id = $this->auth->id;
        $data = CardOrderModel::where([
            'user_id' => $user_id,
            'id' => $id
        ])->with(['card','userCard'])->find();
        if(!$data){
            $this->error('无此订单');
        }

        $BenfitLogModel = new BenfitLogModel();
        $GoodsModel = new GoodsModel();
        if($data['user_card']){
            $data['benfit'] = UserCardBenfitModel::where([
                'user_id' => $user_id,
                'user_card_id' => $data['user_card']['id']
            ])->with('benfit')->select();

            foreach ($data['benfit'] as $key => $value) {
                $value['orignal_nums'] = $BenfitLogModel->where([
                    'user_id' => $user_id,
                    'order_sn' => $data['order_sn'],
                    'benfit_id' => $value['benfit_id'],
                    'type' => 'buy'
                ])->value('nums');

                $value['benfit']['goods_title'] = '';
                if($value['benfit']['goods_id']){
                    $value['benfit']['goods_title'] =  $GoodsModel->where([
                        'id' => $value['benfit']['goods_id'],
                    ])->value('title');
                }
            }
        }

        $this->success('',$data);
    }
}